package com.pet.serve.repository;

import com.pet.serve.entity.Pets;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface PetsRepository extends JpaRepository<Pets, Long> {

    // 根据宠物的状态（售出、在售、寄养、领回）查找宠物
    List<Pets> findByStatus(Pets.PetStatus status);

    // 根据宠物的价格范围查找宠物
    List<Pets> findByPriceBetween(BigDecimal price, BigDecimal price2);

    // 根据宠物ID查找宠物
    Pets findByPetid(Long petid);

    // 根据名字模糊查询宠物
    @Query("SELECT p FROM Pets p WHERE p.name LIKE %:name%")
    List<Pets> findByNameLike(@Param("name") String name);

    // 根据多个条件进行查询，包括种类、状态、价格区间等（动态查询）
    @Query("SELECT p FROM Pets p WHERE (:type IS NULL OR p.type = :type) AND " +
            "(:status IS NULL OR p.status = :status) AND " +
            "(:minPrice IS NULL OR p.price >= :minPrice) AND " +
            "(:maxPrice IS NULL OR p.price <= :maxPrice)")
    List<Pets> findPetsByConditions(@Param("type") String type,
                                    @Param("status") String status,
                                    @Param("minPrice") Double minPrice,
                                    @Param("maxPrice") Double maxPrice);

    // 动态查询，支持分页；使用枚举类型
    @Query("SELECT p FROM Pets p WHERE (:status IS NULL OR p.status = :status) AND " +
            "(COALESCE(:name, '') = '' OR p.name LIKE %:name%) AND " +
            "(:type IS NULL OR p.type = :type) AND " +
            "(:minPrice IS NULL OR p.price >= :minPrice) AND " +
            "(:maxPrice IS NULL OR p.price <= :maxPrice)")
    Page<Pets> findPetsByConditionsWithPagination(@Param("status") Pets.PetStatus status,
                                                  @Param("minPrice") BigDecimal minPrice,
                                                  @Param("maxPrice") BigDecimal maxPrice,
                                                  @Param("name") String name,
                                                  @Param("type") Pets.PetType petType,
                                                  Pageable pageable);
    // 按照分页查询宠物
    @Query("SELECT p FROM Pets p")
    List<Pets> findPetsWithPagination(@Param("page_no") int pageNo, @Param("page_size") int pageSize);


    Page<Pets> findByType(Pets.PetType petType, Pageable pageable);
}
